home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: novice question on copy constru
- Date: 28 Jan 1996 14:17:50 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4eg0ie$ee@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Jan 28, 1996 12:18:22 in article <novice question on copy constru>,
- 'ee_ckmaa@uxmail.ust.hk (Chan Ka Ming)' wrote:
-
-
- >I don't understand why one should create a copy constructor. Doesn't the
- >computer will do the job for you when pass arguments by value? Thanks
- >
- Yes, the compiler generates a copy constructor for you, but with
- default behavior. Often this default causes disaster. To be
- certain that the copy constructor behaves as intended,
- one must write a custom copy constructor.
-
- For example, suppose you have a class that contains a pointer to
- an object, and your destructor frees the memory for that object.
- The default copy constructor copies the pointer to the temporary
- object and, when the temporary is destroyed, it deletes the
- object pointed to. This leaves the original instance of your
- class pointing to a delete'd memory. You can imagine what
- will happen when the destructor for the original object is invoked.
-
- --
- Pete Grant
- Kalevi, Inc.
- Object Oriented Software Development
-